<?php
namespace app\chat\controller;
use think\Controller;
use think\Session;
use think\Request;
class Chatapi extends Controller
{
	/**
	* 按json方式输出通信数据
	* $code 状态码
	* $message 提示信息
	* $data 数据
	* return string
	*/
	public static function json($code,$message = '', $data = array())
	{
		//判断状态码是否正确
		if(!is_numeric($code)){
			return '';
		}
		//拼接数组
		$result = array(
			'code'=>$code,
			'message'=>$message,
			'data'=>$data
		);
		//转换成json格式
		echo json_encode($result);
	}

	/**
	 * 获取token
	 */
	public function getToken()
	{
		// 获取用户id
        $token = input('get.token');
        // var_dump($token);
        $uid = getUid($token);
        // var_dump($uid);
        // 获取token
        // $iconUrl = getIconUrl($uid);
        // $nickname = getNickname($uid);
        // var_dump($nickname);
        // var_dump($iconUrl);die();		
        $token = get_rongcloud_token($uid);
		
        // var_dump($token);die();
        //判断token是否存在
        if($token && $token!=false){
        	$data=array(
	            'token'=>$token
	            );
	        //返回json数据
	        return self::json(200,'获取token成功',$data);
        }else{
        	//token不存在
        	return self::json(400,'获取token失败',null);
        }     
	}
	/**
	** 检查用户在线状态
	**/
	public function checkOnline()
	{
		// 获取用户id
        $token = input('get.token');
        $uid = getUid($token);

        $status = check_user_online($uid);
        
    	if($status == '1'){
    		return self::json(200,'在线');
    	}else if($status == '0'){
    		return self::json(300,'离线');
    	}	
	}

	/**
	 * 更新融云头像
	 */
	public function refresh()
	{
		//修改头像
		
	}
    private function getChatRoomName()
    {
    	$ChatRoomName = Db('chat_room')->where('id',1)->value('chat_room_name');
    	return $ChatRoomName;
    }
	/**
	 * 创建聊天室
	 */
	public function createChatRoom()
	{
		$id = '10001';
		$name = $this->getChatRoomName;
		$chatRoomInfo = cerate_chatroom($id,$name);
		if($chatRoomInfo){
			return self::json(200,'创建聊天室成功');
			//return true;
		}else{
			return self::json(400,'创建聊天室失败');
			//return false;
		}
	}
	/**
	 * 销毁聊天室
	 */
	public function destroyChatRoom()
	{
		$id = '10001';
		$chatRoomInfo = destroy_chatroom($id);
		if($chatRoomInfo){
			return self::json(200,'销毁聊天室成功');
		}else{
			return self::json(400,'销毁聊天室失败');
		}
	}
	/**
	 * 查询
	 聊天室信息
	 */
	public function queryChatRoom()
	{
		$id = '10001';
		$chatRoomInfo = query_chatroom($id);
		return self::json(200,'查询聊天室信息成功',$chatRoomInfo);
        
	}
	/**
	 * 查询聊天室信息
	 */
    private function getChatRoomInfo()
    {
    	$id = '10001';
		$chatRoomInfo = query_chatroom($id);
		return $chatRoomInfo;
    }
	/**
	 * 加入聊天室
	 */
	public function joinChatRoom()
	{
		$chatRoomInfo = $this->getChatRoomInfo();
		$count = count($chatRoomInfo);
		if($count == 0 ){
			 $id = '10001';
		     $name = $this->getChatRoomName();
		     $RoomInfo = cerate_chatroom($id,$name);
		}
		// 获取用户id
        $token = input('get.token');
        $uid = getUid($token);
		$id = '10001';
		$result = join_chatroom($uid,$id);
		if($result){
			return self::json(200,'加入成功');
		}else{
			return self::json(400,'加入失败');
		}
	}

	/**
	 * 查询聊天室成员
	 */
	public function queryUser()
	{
		$chatroomId = '10001';
		$chatRoomUsers = query_chatroom_users($chatroomId);
		//
		return self::json(200,'查询聊天室成员成功',$chatRoomUsers);
	}

	/**
	 *  用户禁言
	 */
	public function addGagUser()
	{
		// 获取用户id
        $token = input('get.token');
        $userId = getUid($token);
		$chatroomId = '10001';
		$minute = '60';
		$result = add_chatroom_gag_user($userId,$chatroomId,$minute);
		if($result){
			return self::json(200,'禁言成功');
		}else{
			return self::json(400,'禁言失败');
		}
	}
	/**
	 * 用户解禁
	 */
	public function gagRollback()
	{
		// 获取用户id
        $token = input('get.token');
        $userId = getUid($token);
		$chatroomId = '10001';
		$result = chatroom_gag_rollback($userId,$chatroomId);
		if($result){
			return self::json(200,'禁言成功');
		}else{
			return self::json(400,'禁言失败');
		}
	}
	/**
	 * 添加敏感词汇
	 */
	public function addWordFilter()
	{
		$word = input('get.word');
		$result = add_wordfilter($word);
		if($result){
			return self::json(200,'添加敏感词汇成功');
		}else{
			return self::json(400,'添加敏感词汇失败');
		}
	}
	/**
	 * 移除敏感词汇
	 */
	public function deleteWordFiler()
	{
		$word = input('get.word');
		$result = delete_wordfilter($word);
		if($result){
			return self::json(200,'移除敏感词汇成功');
		}else{
			return self::json(400,'移除敏感词汇失败');
		}
	}
	/**
	 * 查询敏感词汇
	 */
	public function getWordFiler()
	{
		$WordFiler = get_wordfilter();
		return self::json(200,'查询敏感词汇成功',$WordFiler);

	}
	/**
	 * 发送聊天室消息方法
	 */
	public function publishChatRoom()
	{
		$token = input('get.token');
        $fromUserId = getUid($token);
		$toChatroomId = '10001';
		$objectName = input('get.type');
		// $objectName = 'RC:TxtMsg';
		$content = input('get.content');
		$result = publish_chatroom($fromUserId,$toChatroomId,$objectName,$content);
		if($result){
			return self::json(200,'消息发送成功');
		}else{
			return self::json(400,'消息发送失败');
		}
	}
	/**
	 * 添加聊天室消息优先级 方法
	 */
	public function addPriority()
	{
		$priority = array(
			0 => 'RC:TxtMsg',
			1 => 'RC:ImgMsg',
			2 => 'RC:VcMsg',
			3 => 'RC:ImgTextMsg'
			);
		$result = add_priority($priority);
		if($result){
			return self::json(200,'添加聊天室消息优先级成功');
		}else{
			return self::json(400,'添加聊天室消息优先级失败');
		}
	}
	/**
	 * 删除聊天室消息优先级 方法
	 */
	public function removePriority()
	{
		$priority = ['RC:ImgTextMsg'];
		$result = remove_priority($priority);
		if($result){
			return self::json(200,'删除聊天室消息优先级成功');
		}else{
			return self::json(400,'删除聊天室消息优先级失败');
		}
	}
	/**
	 * 查询聊天室消息优先级 方法
	 */
	public function queryPriority()
	{
		$priority = ['RC:TxtMsg','RC:ImgMsg','RC:VcMsg','RC:ImgTextMsg'];
		$objectNames = query_priority($priority);
		return self::json(200,'查询聊天室消息优先级成功',$objectNames);	
	}
	/**
	 * 添加聊天室白名单成员 方法
	 */
	public function addWhitelist()
	{
		$token = input('get.token');
        $userId = getUid($token);
		$chatroomId = '10001';
		$result = add_chatroom_whitelist($chatroomId,$userId);
		if($result){
			return self::json(200,'添加聊天室白名单成员成功');
		}else{
			return self::json(400,'添加聊天室白名单成员失败');
		}
	}
	/**
	** 返回APP_SECRET
	**/
	public function getAppSecret()
	{
		$token = input('get.token');
        $userId = getUid($token);
        $usersId = $this->queryWhitelist();
        $AppSecret = $this->returnAppSecret( $userId,$usersId);
        if($AppSecret){
        	$data = array(
        			'app_secret' => $AppSecret
        		);
        	return self::json(200,'已付费',$data); 
        }else{
        	return self::json(400,'未付费');
        }
        
        
	}
	/**
	**
	**/
	private function returnAppSecret($userId,$usersId)
	{
		if($usersId){
			foreach($usersId as $k => $v){
				if($v == $userId){
					return Config('APP_SECRET');
				}else{
					return '';
				}
			}
		}else{
			return '';
		}
		
	}
	/**
	 * 查询聊天室白名单成员 方法
	 */
	private function queryWhitelist()
	{
		$chatroomId = '10001';
		$users = query_chatroom_whitelist($chatroomId);
		return $users;
	}
	/**
	 * 移除聊天室白名单成员
	 */
	public function delWhitelist()
	{
		$token = input('get.token');
        $userId = getUid($token);
		$chatroomId = '10001';
		$result = del_chatroom_whitelist($chatroomId,$userId);
		if($result){
			return self::json(200,'移除聊天室白名单成员成功');
		}else{
			return self::json(400,'移除聊天室白名单成员失败');
		}
	}
}